<?php
/**
 * 测试SKU API接口
 * 用于验证通过商品ID和规格获取价格及库存的功能
 */

// 设置错误报告
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 引入必要的文件
require_once __DIR__ . '/backend/vendor/autoload.php';

use app\model\GoodsModel;
use app\model\GoodsKuModel;
use app\model\GoodsSpecModel;

echo "=== SKU API 功能测试 ===\n\n";

// 测试1: 检查是否有商品数据
echo "1. 检查商品数据...\n";
$goods = GoodsModel::first();
if ($goods) {
    echo "   找到商品: ID={$goods->id}, 标题={$goods->title}\n";
    $goodsId = $goods->id;
} else {
    echo "   没有找到商品数据，请先添加商品\n";
    exit;
}

// 测试2: 检查商品规格
echo "\n2. 检查商品规格...\n";
$specs = GoodsSpecModel::where('goods_id', $goodsId)->get();
if ($specs->count() > 0) {
    echo "   找到 {$specs->count()} 个规格:\n";
    foreach ($specs as $spec) {
        echo "   - {$spec->spec_name}: " . implode(', ', explode('@', $spec->spec_value)) . "\n";
    }
} else {
    echo "   该商品没有规格数据\n";
    exit;
}

// 测试3: 检查SKU数据
echo "\n3. 检查SKU数据...\n";
$skus = GoodsKuModel::where('goods_id', $goodsId)->get();
if ($skus->count() > 0) {
    echo "   找到 {$skus->count()} 个SKU:\n";
    foreach ($skus as $sku) {
        echo "   - 规格: {$sku->specs}, 价格: {$sku->price}, 库存: {$sku->v}\n";
    }
} else {
    echo "   该商品没有SKU数据\n";
    exit;
}

// 测试4: 测试根据规格组合查找SKU
echo "\n4. 测试根据规格组合查找SKU...\n";
$firstSku = $skus->first();
if ($firstSku) {
    $specsString = $firstSku->specs;
    echo "   测试规格组合: {$specsString}\n";
    
    $foundSku = GoodsKuModel::findBySpecs($goodsId, $specsString);
    if ($foundSku) {
        echo "   找到SKU: ID={$foundSku->id}, 价格={$foundSku->price}, 库存={$foundSku->v}\n";
    } else {
        echo "   未找到SKU\n";
    }
}

// 测试5: 测试SKU编码生成
echo "\n5. 测试SKU编码生成...\n";
if ($firstSku) {
    $skuCode = GoodsKuModel::generateSkuCode($goodsId, $firstSku->specs);
    echo "   生成的SKU编码: {$skuCode}\n";
    
    // 测试根据SKU编码查找
    $foundByCode = GoodsKuModel::findBySkuCode($skuCode);
    if ($foundByCode) {
        echo "   根据编码找到SKU: ID={$foundByCode->id}, 规格={$foundByCode->specs}\n";
    } else {
        echo "   根据编码未找到SKU\n";
    }
}

// 测试6: 测试API接口模拟
echo "\n6. 测试API接口模拟...\n";
echo "   模拟调用 /v1/goods/get-sku-by-specs\n";
echo "   参数: goods_id={$goodsId}, specs={$specsString}\n";

// 模拟API调用
try {
    $sku = GoodsKuModel::findBySpecs($goodsId, $specsString);
    if ($sku) {
        $response = [
            'data' => [
                'id' => $sku->id,
                'goods_id' => $sku->goods_id,
                'specs' => $sku->specs,
                'price' => (float)$sku->price,
                'stock' => (int)$sku->v,
                'created_at' => $sku->created_at,
                'updated_at' => $sku->updated_at
            ],
            'msg' => '获取成功',
            'code' => 1
        ];
        echo "   返回数据: " . json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
    } else {
        echo "   返回数据: {\"data\": null, \"msg\": \"该规格组合不存在\", \"code\": 0}\n";
    }
} catch (Exception $e) {
    echo "   错误: " . $e->getMessage() . "\n";
}

echo "\n=== 测试完成 ===\n";
echo "\n使用方法:\n";
echo "1. 小程序前端调用: GoodsService.getSkuBySpecs(goodsId, specs)\n";
echo "2. 规格组合格式: \"红色@XL@棉质\" (用@分隔)\n";
echo "3. 返回数据包含: 价格(price)和库存(stock)\n";
echo "4. 支持的其他接口:\n";
echo "   - getSkuList(goodsId): 获取商品所有SKU\n";
echo "   - getSkuByCode(skuCode): 根据SKU编码获取SKU\n";
?>
